package cn.wang.web.cargo;

import cn.wang.entity.cargo.ContractProduct;

import cn.wang.entity.cargo.ExtCproduct;
import cn.wang.entity.cargo.Factory;
import cn.wang.service.cargo.ContractProductService;
import cn.wang.service.cargo.ContractService;
import cn.wang.service.cargo.ExtCproductService;
import cn.wang.utils.Pagination;
import cn.wang.web.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Set;

/**
 * Created by john on 2017/11/1.
 */
@Controller
public class ExtCproductAction extends BaseAction implements ModelDriven<ExtCproduct> {
    private Pagination page = new Pagination();
    private List<ExtCproduct> extproductList;
    private Set<ExtCproduct> productSet;
    private ExtCproduct model = new ExtCproduct();
    private List<Factory> factoryList;

    @Autowired
    private ExtCproductService cproductService;
    @Autowired
    private ContractProductService contractProductService;

    //分页
    public String list() {
        page = cproductService.findPage(page.getPageNo(), page.getPageSize());
        page.setUrl("extCproductAction_list");
        push(page);

        return "tocreate";
    }

    //查询所有厂商 分页和添加在同一页 第一次访问tocreate 实现分页 第二访问list()
    public String tocreate() {
        factoryList = contractProductService.findAll();

        page = cproductService.findPage1(page.getPageNo(), page.getPageSize(), model.getContractProduct().getId());
        page.setUrl("extCproductAction_list");
        push(page);
        return "tocreate";
    }

    //查寻单个部门信息
    public String toview() {

        return "toview";
    }

    //添加附件
    public String insert() {
        cproductService.save(model);
        return tocreate();

    }

    //更新货物 先查询老数据
    public String toupdate() {
        //厂家列表
        factoryList = contractProductService.findAll();
        ExtCproduct extCproduct = cproductService.findById(model.getId());
        push(extCproduct);
        return "toupdate";
    }

    public String update() {
        cproductService.update(model);
        return tocreate();

    }

    //删除 附件 多个部门删除时 id 字符类型id会 拼接在一起(,空格)
    public String delete() {
        cproductService.deleteById(model);
        return tocreate();
    }

    public void setPage(Pagination page) {
        this.page = page;
    }


    public List<ExtCproduct> getExtproductList() {
        return extproductList;
    }

    public void setExtproductList(List<ExtCproduct> extproductList) {
        this.extproductList = extproductList;
    }

    @Override
    public ExtCproduct getModel() {
        //将字段名设为model使一行代码两用 1模型驱动 2 字段set方法
        return model;
    }

    public List<Factory> getFactoryList() {
        return factoryList;
    }
}
